﻿using Dapper;
using Logistics.Commn;
using Logistics.EntityFrameworkCore;
using Logistics.Model;
using MySqlConnector;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Domain.Repositories.Dapper;
using Volo.Abp.EntityFrameworkCore;

namespace Logistics.examination
{
    public class ApprovalActivityRepository: DapperRepository<LogisticsDbContext>, IApprovalActivityRepository
    {
        private readonly LogisticsDbContext db;
        readonly string SqlConn = "Server=10.31.64.9;Port=3306;Database=Logistics;Uid=si;Pwd=111111;";

        public ApprovalActivityRepository(LogisticsDbContext _db, IDbContextProvider<LogisticsDbContext> dbContextProvider) : base(dbContextProvider)
        {
            db = _db;
        }

        public async Task<int> Create(int id, int TId)
        {
            //接受登录传送过来的值
            var user= RedisHelper.Get<Operator>("Operator");

            var process = db.ApprovalProcess.FirstOrDefault(a => a.Id == 1);
            //把配置表弄为空
            List<ProcessConfiguration> pc = null;
            using (IDbConnection con = new MySqlConnection(SqlConn))
            {
                //查询出类型
                var sql = $@"select * from `ProcessConfiguration` where ProcessId={TId}";
                pc = con.Query<ProcessConfiguration>(sql).ToList();
            }
            //生成GuId
            var guid = Guid.NewGuid().ToString();
            //循环添加
            foreach (var item in pc)
            {
                var Appac = new ApprovalActivity()
                {
                   ProcessId=item.ProcessId,
                    NodeId=item.ConditionId,
                    RoleSector=item.RoleSector,
                    ApprovalRoleId=item.ApprovalRoleId,
                    ApprovalUserId=item.ApprovalUserId,
                    BasId = 1,
                    ApprovalUser = user.Account,
                    AuditProposal="",
                    AuditStatus='0',
                    AuditTime= DateTime.Now,
                    Fid= TId,
                    DataId=id
                };
                db.ApprovalActivity.Add(Appac);
            }
            return db.SaveChanges();
        }

        public async Task<List<ApprovalActivity>> GetApprovalActivity()
        {
            using (IDbConnection con = new MySqlConnection(SqlConn))
            {
                var user = RedisHelper.Get<Operator>("Operator");
                var Rid = user.RoleId;
                var sql = $@"select a.ContractTitle Name,b.*,c.Name TName from cargoowner a join approvalactivity b on a.Id=b.DataId join approvalprocess c on b.Fid=c.Id where b.Fid=1 and b.ApprovalRoleId={Rid} union select a.ContractTitle Name, b.*,c.Name TName from Carry a join approvalactivity b on a.Id = b.DataId join approvalprocess c on b.Fid = c.Id where b.Fid = 2 and b.ApprovalRoleId={Rid} union select a.ContractTitle Name, b.*,c.Name TName from Currency a join approvalactivity b on a.Id = b.DataId join approvalprocess c on b.Fid = c.Id where b.Fid = 3 and b.ApprovalRoleId={Rid} union select a.Name Name, b.*,c.Name TName from Procurement a join approvalactivity b on a.Id = b.DataId join approvalprocess c on b.Fid = c.Id where b.Fid = 4 and b.ApprovalRoleId={Rid} union select a.Title Name, b.*,c.Name TName from Claim a join approvalactivity b on a.Id = b.DataId join approvalprocess c on b.Fid = c.Id where b.Fid = 5 and b.ApprovalRoleId={Rid} union select a.Title Name, b.*,c.Name TName from Payment a join approvalactivity b on a.Id = b.DataId join approvalprocess c on b.Fid = c.Id where b.Fid = 6 and b.ApprovalRoleId={Rid} union select d.Name Name, b.*,c.Name TName from Entry a join approvalactivity b on a.Id = b.DataId join approvalprocess c on b.Fid = c.Id join Basic d on a.Xid = d.Id where b.Fid = 7 and b.ApprovalRoleId={Rid} union select d.Name Name, b.*,c.Name TName from Dimission a join approvalactivity b on a.Id = b.DataId join approvalprocess c on b.Fid = c.Id join Basic d on a.Xid = d.Id where b.Fid = 8 and b.ApprovalRoleId={Rid} union select d.Name Name, b.*,c.Name TName from Become a join approvalactivity b on a.Id = b.DataId join approvalprocess c on b.Fid = c.Id join Basic d on a.Xid = d.Id where b.Fid = 9 and b.ApprovalRoleId={Rid}";
               var pc = con.Query<ApprovalActivity>(sql).ToList();
                return pc;
            }
           
        }
        /// <summary>
        /// 审批状态修改
        /// </summary>
        /// <param name="id"></param>
        /// <param name="TId"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<int> UpdateA(int id, int TId,int Did,int AuditStatus,string AuditProposal)
        {
            using (IDbConnection con = new MySqlConnection(SqlConn))
            {
                var sql1 = $@" update ApprovalActivity set AuditStatus={AuditStatus},AuditProposal='{AuditProposal}' where Id={id} ";
                var i = con.Execute(sql1);
                if(TId==1)
                {
                    var w = $@" update Cargoowner set State={AuditStatus} where Id={Did} ";
                    var q = con.Execute(w);
                    return q;
                }
                if (TId == 2)
                {
                    var w = $@" update Carry set State={AuditStatus} where Id={Did} ";
                    var e = con.Execute(w);
                    return e;
                }
                if (TId == 3)
                {
                    var w = $@" update Currency set State={AuditStatus} where Id={Did} ";
                    var z = con.Execute(w);
                    return z;
                }
                if (TId == 4)
                {
                    var w = $@" update Procurement set State={AuditStatus} where Id={Did} ";
                    var x = con.Execute(w);
                    return x;
                }
                if (TId == 5)
                {
                    var w = $@" update Claim set State={AuditStatus} where Id={Did} ";
                    var x = con.Execute(w);
                    return x;
                }
                if (TId == 7)
                {
                    var w = $@" update Entry set Ezt={AuditStatus} where Id={Did} ";
                    var x = con.Execute(w);
                    return x;
                }
                if (TId == 8)
                {
                    var w = $@" update Dimission set Ezt={AuditStatus} where Id={Did} ";
                    var x = con.Execute(w);
                    return x;
                }
                if (TId == 9)
                {
                    var w = $@" update Become set Ezt={AuditStatus} where Id={Did} ";
                    var x = con.Execute(w);
                    return x;
                }
                return 0;
            }
        }

        public async Task<int> UpdateB(int id, int AuditStatus, string AuditProposal)
        {
            using (IDbConnection con = new MySqlConnection(SqlConn))
            {
                var sql1 = $@" update ApprovalActivity set AuditStatus={AuditStatus},AuditProposal='{AuditProposal}' where Id={id} ";
                var i = con.Execute(sql1);

                return i;
            }
        }
        public async Task<int> UpdateC(int id, int TId, int Did)
        {
            using (IDbConnection con = new MySqlConnection(SqlConn))
            {
                var sql1 = $@" update ApprovalActivity set AuditStatus='2' where Id={id} ";
                var i = con.Execute(sql1);
                if (TId == 1)
                {
                    var w = $@" update Cargoowner set State=3 where Id={Did} ";
                    var q = con.Execute(w);
                    return q;
                }
                if (TId == 2)
                {
                    var w = $@" update Carry set State=3 where Id={Did} ";
                    var e = con.Execute(w);
                    return e;
                }
                if (TId == 2)
                {
                    var w = $@" update Carry set State=3 where Id={Did} ";
                    var x = con.Execute(w);
                    return x;
                }
                if (TId == 3)
                {
                    var w = $@" update Currency set State=3 where Id={Did} ";
                    var z = con.Execute(w);
                    return z;
                }
                if (TId == 4)
                {
                    var w = $@" update Procurement set State=3 where Id={Did} ";
                    var x = con.Execute(w);
                    return x;
                }
                if (TId == 5)
                {
                    var w = $@" update Claim set State=3 where Id={Did} ";
                    var x = con.Execute(w);
                    return x;
                }
                if (TId == 7)
                {
                    var w = $@" update Entry set Ezt=3 where Id={Did} ";
                    var x = con.Execute(w);
                    return x;
                }
                if (TId == 8)
                {
                    var w = $@" update Dimission set Ezt=3 where Id={Did} ";
                    var x = con.Execute(w);
                    return x;
                }
                if (TId == 9)
                {
                    var w = $@" update Become set Ezt=3 where Id={Did} ";
                    var x = con.Execute(w);
                    return x;
                }
                return 0;
            }
        }

        public async Task<int> UpdateD(int id)
        {
            using (IDbConnection con = new MySqlConnection(SqlConn))
            {
                var sql1 = $@" update ApprovalActivity set AuditStatus='3' where Id={id} ";
                var i = con.Execute(sql1);

                return i;
            }
        }
    }
}
